<template>
  <a-layout class="admin-layout">
    <!-- 侧边栏 -->
    <a-layout-sider v-model:collapsed="collapsed" :trigger="null" collapsible class="sider">
      <div class="logo">
        <span class="logo-icon">⚡</span>
        <span v-if="!collapsed" class="logo-text">管理后台</span>
      </div>
      <a-menu
        v-model:selectedKeys="selectedKeys"
        mode="inline"
        :items="menuItems"
        @click="handleMenuClick"
      />
    </a-layout-sider>

    <!-- 右侧内容区 -->
    <a-layout>
      <!-- 顶部导航 -->
      <a-layout-header class="header">
        <div class="header-left">
          <menu-unfold-outlined
            v-if="collapsed"
            class="trigger"
            @click="() => (collapsed = !collapsed)"
          />
          <menu-fold-outlined v-else class="trigger" @click="() => (collapsed = !collapsed)" />
        </div>
        <div class="header-right">
          <span class="user-info">{{ userStore.userInfo?.nickname }}</span>
          <a-button type="link" @click="goToFrontend">返回前台</a-button>
          <a-button type="link" @click="handleLogout">退出登录</a-button>
        </div>
      </a-layout-header>

      <!-- 主体内容 -->
      <a-layout-content class="content">
        <router-view />
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script setup lang="ts">
import { ref, h } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { message } from 'ant-design-vue'
import {
  MenuFoldOutlined,
  MenuUnfoldOutlined,
  UserOutlined,
  CodeOutlined,
  TagsOutlined,
} from '@ant-design/icons-vue'
import type { MenuProps } from 'ant-design-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const collapsed = ref(false)
const selectedKeys = ref<string[]>([route.path])

// 菜单项
const menuItems: MenuProps['items'] = [
  {
    key: '/admin/user',
    icon: () => h(UserOutlined),
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/code',
    icon: () => h(CodeOutlined),
    label: '代码管理',
    title: '代码管理',
  },
  {
    key: '/admin/tag',
    icon: () => h(TagsOutlined),
    label: '标签管理',
    title: '标签管理',
  },
]

const handleMenuClick = ({ key }: { key: string }) => {
  router.push(key)
}

const goToFrontend = () => {
  router.push('/')
}

const handleLogout = async () => {
  await userStore.logout()
  message.success('退出登录成功')
  router.push('/login')
}
</script>

<style scoped>
.admin-layout {
  min-height: 100vh;
}

.sider {
  background: linear-gradient(180deg, #1a1f3a 0%, #0f1229 100%);
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.3);
}

.logo {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  border-bottom: 1px solid rgba(74, 158, 255, 0.2);
}

.logo-icon {
  font-size: 24px;
}

.logo-text {
  font-size: 18px;
  font-weight: bold;
  background: linear-gradient(135deg, #4a9eff 0%, #6bb3ff 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header {
  background: linear-gradient(135deg, #1a1f3a 0%, #0f1229 100%);
  border-bottom: 1px solid rgba(74, 158, 255, 0.2);
  padding: 0 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.header-left {
  display: flex;
  align-items: center;
}

.trigger {
  font-size: 18px;
  cursor: pointer;
  transition: color 0.3s;
  color: #e0e0e0;
}

.trigger:hover {
  color: #4a9eff;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-info {
  color: #e0e0e0;
  margin-right: 8px;
}

.content {
  margin: 24px;
  padding: 24px;
  background: #1a1f3a;
  border-radius: 8px;
  min-height: calc(100vh - 64px - 48px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}
</style>

